<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>流式输出接口测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
        }
        .test-buttons {
            display: flex;
            flex-direction: column;
            gap: 10px;
            margin-bottom: 20px;
        }
        button {
            padding: 12px 20px;
            background-color: #007cba;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #005a87;
        }
        button:disabled {
            background-color: #cccccc;
            cursor: not-allowed;
        }
        #output {
            background-color: #f8f8f8;
            border: 1px solid #ddd;
            border-radius: 4px;
            padding: 15px;
            min-height: 300px;
            max-height: 500px;
            overflow-y: auto;
            margin-top: 20px;
            white-space: pre-wrap;
            font-family: monospace;
        }
        .status {
            padding: 10px;
            border-radius: 4px;
            margin: 10px 0;
        }
        .status.connecting {
            background-color: #fff3cd;
            border: 1px solid #ffeaa7;
            color: #856404;
        }
        .status.connected {
            background-color: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        .status.error {
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
        .test-section {
            margin-bottom: 30px;
            padding: 15px;
            border: 1px solid #eee;
            border-radius: 4px;
        }
        .test-section h2 {
            margin-top: 0;
            color: #333;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>流式输出接口测试</h1>
        
        <div class="test-section">
            <h2>直接PHP脚本测试</h2>
            <div class="test-buttons">
                <button id="testMinimalBtn">测试 Minimal Stream</button>
                <button id="testImprovedBtn">测试 Improved Stream</button>
            </div>
        </div>
        
        <div class="test-section">
            <h2>ThinkPHP控制器测试</h2>
            <div class="test-buttons">
                <button id="testApiMinimalBtn">测试 /api/stream_test/minimal</button>
                <button id="testApiEventsBtn">测试 /api/stream_test/events</button>
            </div>
        </div>
        
        <div class="test-section">
            <h2>流式输出诊断测试</h2>
            <div class="test-buttons">
                <button id="testDiagnosticsBasicBtn">测试 /api/streaming_diagnostics/basic</button>
                <button id="testDiagnosticsEventsBtn">测试 /api/streaming_diagnostics/with_events</button>
                <button id="testDiagnosticsLongBtn">测试 /api/streaming_diagnostics/long_running</button>
                <button id="testDiagnosticsBeanbunsBtn">测试 /api/streaming_diagnostics/beanbuns_style</button>
            </div>
        </div>
        
        <div class="test-section">
            <h2>豆包AI流式接口测试</h2>
            <div class="test-buttons">
                <button id="testDoubaoBtn">测试 /api/beanbuns_stream/chat</button>
            </div>
        </div>
        
        <button id="stopBtn" disabled>停止连接</button>
        
        <div id="status" class="status connecting" style="display: none;">
            状态信息将显示在这里
        </div>
        
        <div id="output"></div>
    </div>

    <script>
        const testMinimalBtn = document.getElementById('testMinimalBtn');
        const testImprovedBtn = document.getElementById('testImprovedBtn');
        const testApiMinimalBtn = document.getElementById('testApiMinimalBtn');
        const testApiEventsBtn = document.getElementById('testApiEventsBtn');
        const testDiagnosticsBasicBtn = document.getElementById('testDiagnosticsBasicBtn');
        const testDiagnosticsEventsBtn = document.getElementById('testDiagnosticsEventsBtn');
        const testDiagnosticsLongBtn = document.getElementById('testDiagnosticsLongBtn');
        const testDiagnosticsBeanbunsBtn = document.getElementById('testDiagnosticsBeanbunsBtn');
        const testDoubaoBtn = document.getElementById('testDoubaoBtn');
        const stopBtn = document.getElementById('stopBtn');
        const statusDiv = document.getElementById('status');
        const outputDiv = document.getElementById('output');
        
        let eventSource = null;
        
        // 更新状态显示
        function updateStatus(message, type = 'connecting') {
            statusDiv.textContent = message;
            statusDiv.className = 'status ' + type;
            statusDiv.style.display = 'block';
        }
        
        // 清空输出
        function clearOutput() {
            outputDiv.textContent = '';
        }
        
        // 开始流式测试
        function startStreaming(url) {
            // 清空输出
            clearOutput();
            
            // 禁用所有按钮，启用停止按钮
            disableAllButtons();
            stopBtn.disabled = false;
            
            updateStatus('正在连接到: ' + url, 'connecting');
            
            // 创建EventSource连接
            eventSource = new EventSource(url);
            
            // 连接打开事件
            eventSource.onopen = function(event) {
                updateStatus('连接已建立，正在接收数据...', 'connected');
            };
            
            // 接收数据事件
            eventSource.onmessage = function(event) {
                outputDiv.textContent += 'data: ' + event.data + '\n\n';
                outputDiv.scrollTop = outputDiv.scrollHeight;
            };
            
            // 接收自定义事件
            eventSource.addEventListener('start', function(event) {
                const data = JSON.parse(event.data);
                outputDiv.textContent += 'event: start\ndata: ' + data.content + '\n\n';
                outputDiv.scrollTop = outputDiv.scrollHeight;
                updateStatus('开始接收数据...', 'connected');
            });
            
            eventSource.addEventListener('message', function(event) {
                outputDiv.textContent += 'event: message\ndata: ' + event.data + '\n\n';
                outputDiv.scrollTop = outputDiv.scrollHeight;
            });
            
            eventSource.addEventListener('progress', function(event) {
                outputDiv.textContent += 'event: progress\ndata: ' + event.data + '\n\n';
                outputDiv.scrollTop = outputDiv.scrollHeight;
            });
            
            eventSource.addEventListener('end', function(event) {
                const data = JSON.parse(event.data);
                outputDiv.textContent += 'event: end\ndata: ' + data.content + '\n\n';
                outputDiv.scrollTop = outputDiv.scrollHeight;
                updateStatus('数据接收完成', 'connected');
                stopStreaming();
            });
            
            eventSource.addEventListener('content', function(event) {
                const data = JSON.parse(event.data);
                outputDiv.textContent += 'event: content\ndata: ' + data.content + '\n\n';
                outputDiv.scrollTop = outputDiv.scrollHeight;
            });
            
            eventSource.addEventListener('processed', function(event) {
                const data = JSON.parse(event.data);
                outputDiv.textContent += 'event: processed\ndata: ' + data.content + '\n\n';
                outputDiv.scrollTop = outputDiv.scrollHeight;
            });
            
            eventSource.addEventListener('stats', function(event) {
                const data = JSON.parse(event.data);
                outputDiv.textContent += 'event: stats\ndata: ' + JSON.stringify(data) + '\n\n';
                outputDiv.scrollTop = outputDiv.scrollHeight;
            });
            
            // 错误事件
            eventSource.addEventListener('error', function(event) {
                updateStatus('连接错误: ' + (event.data || '未知错误'), 'error');
                stopStreaming();
            });
            
            // 连接错误
            eventSource.onerror = function(event) {
                updateStatus('连接错误，请检查网络或服务器状态', 'error');
                stopStreaming();
            };
        }
        
        // 停止流式连接
        function stopStreaming() {
            if (eventSource) {
                eventSource.close();
                eventSource = null;
            }
            
            // 启用所有按钮，禁用停止按钮
            enableAllButtons();
            stopBtn.disabled = true;
        }
        
        // 禁用所有测试按钮
        function disableAllButtons() {
            testMinimalBtn.disabled = true;
            testImprovedBtn.disabled = true;
            testApiMinimalBtn.disabled = true;
            testApiEventsBtn.disabled = true;
            testDiagnosticsBasicBtn.disabled = true;
            testDiagnosticsEventsBtn.disabled = true;
            testDiagnosticsLongBtn.disabled = true;
            testDiagnosticsBeanbunsBtn.disabled = true;
            testDoubaoBtn.disabled = true;
        }
        
        // 启用所有测试按钮
        function enableAllButtons() {
            testMinimalBtn.disabled = false;
            testImprovedBtn.disabled = false;
            testApiMinimalBtn.disabled = false;
            testApiEventsBtn.disabled = false;
            testDiagnosticsBasicBtn.disabled = false;
            testDiagnosticsEventsBtn.disabled = false;
            testDiagnosticsLongBtn.disabled = false;
            testDiagnosticsBeanbunsBtn.disabled = false;
            testDoubaoBtn.disabled = false;
        }
        
        // 绑定事件
        testMinimalBtn.addEventListener('click', function() {
            startStreaming('/minimal_stream_test.php');
        });
        
        testImprovedBtn.addEventListener('click', function() {
            startStreaming('/improved_stream_test.php');
        });
        
        testApiMinimalBtn.addEventListener('click', function() {
            startStreaming('/api/stream_test/minimal');
        });
        
        testApiEventsBtn.addEventListener('click', function() {
            startStreaming('/api/stream_test/events');
        });
        
        testDiagnosticsBasicBtn.addEventListener('click', function() {
            startStreaming('/api/streaming_diagnostics/basic');
        });
        
        testDiagnosticsEventsBtn.addEventListener('click', function() {
            startStreaming('/api/streaming_diagnostics/with_events');
        });
        
        testDiagnosticsLongBtn.addEventListener('click', function() {
            startStreaming('/api/streaming_diagnostics/long_running');
        });
        
        testDiagnosticsBeanbunsBtn.addEventListener('click', function() {
            startStreaming('/api/streaming_diagnostics/beanbuns_style');
        });
        
        testDoubaoBtn.addEventListener('click', function() {
            startStreaming('/api/beanbuns_stream/chat?question=你好世界&stream_mode=char');
        });
        
        stopBtn.addEventListener('click', stopStreaming);
        
        // 页面卸载时关闭连接
        window.addEventListener('beforeunload', function() {
            if (eventSource) {
                eventSource.close();
            }
        });
    </script>
</body>
</html>